﻿using JWT;
using MySSO.Core.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MySSOMVC
{
    public class IdtokenHelper
    {
        public static string GenerateIdtoken(AdminUserEntity user, string token, int expireseconds, string secret)
        {
            IDateTimeProvider provider = new UtcDateTimeProvider();
            var now = provider.GetNow();

            var unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); // or use JwtValidator.UnixEpoch
            var secondsSinceEpoch = Math.Round((now - unixEpoch).TotalSeconds);

            var payload = new Dictionary<string, object>
            {
                { "exp", secondsSinceEpoch+expireseconds },
                {"token",token },
                {"uid", user.AUID},
                {"name",user.Name }
            };
            string id_token=JWTHelper.Encode(payload, secret);
            return id_token;

        }

        public static string RefreshIdtoken(IDictionary<string, object> oldpayload, int expireseconds, string secret)
        {

            IDateTimeProvider provider = new UtcDateTimeProvider();
            var now = provider.GetNow();

            var unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); // or use JwtValidator.UnixEpoch
            var secondsSinceEpoch = Math.Round((now - unixEpoch).TotalSeconds);

            var payload = new Dictionary<string, object>
            {
                { "exp", secondsSinceEpoch+expireseconds },
                {"token",oldpayload["token"] },
                {"uid", oldpayload["uid"]},
                {"name",oldpayload["name"] }
            };
            string id_token = JWTHelper.Encode(payload, secret);
            return id_token;

        }
    }
}